const axios = require('axios');

const { PubMedConfig } = require('./config');
const { abstractHandler } = require('./util');

/** 根据关键字查询列表数据
 * @param {String} keyword 关键字
 */
async function requestPMIDListData(keyword) {
  const result = await axios.get(`${PubMedConfig.baseUrl}/esearch.fcgi`, {
    params: {
      ...PubMedConfig.params,
      term: `${keyword}[Title/Abstract]`,
      // retmax: 10,
      retmax: 100,
      // retmax: 9999,
    },
  });
  console.log(result?.data?.esearchresult?.idlist);
  console.log(result?.data?.esearchresult?.idlist?.length);
  return result?.data?.esearchresult?.idlist || [];
}

/** 根据 id 获取摘要信息
 * @param {String} pmid PMID 列表数据
 */
async function requestAbstractFromPMID(pmid) {
  const result = await axios.get(`${PubMedConfig.baseUrl}/efetch.fcgi`, {
    params: {
      ...PubMedConfig.params,
      id: pmid,
      rettype: 'abstract',
      retmode: 'xml',
    },
  });
  await abstractHandler(pmid, result);
}

module.exports = { requestPMIDListData, requestAbstractFromPMID };
